<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use app\common\model\Company;
use app\common\model\Member;
use app\common\model\ApplyPic;
use app\common\model\Apply;

use app\common\logic\Logic;
use think\Request;
class Seller extends Admin
{
    protected $modelClass = 'app\common\model\Company';
    
    public function index()
    {
    
    }
    
    public function apply(){
        $logic = new Logic();
        $logic->setValue('modelClass','app\common\model\Apply');
        $logic->setValue('paginate',1);
        $logic->setValue('size',20);
        $params = paramForm();
        
        if(isset($params['id'])){
            $logic->setValue('with','pic,user');
            $apply = $logic->findModel($params['id']);
            $this->assign('model',$apply);
            
            return $this->fetch('apply/detail');
            die;
        }
        
        if(Request::instance()->isAjax()){
            $logic->setValue('with','user');
            $list = $logic->prepareDataProvider();
            success($list);
        }else{
            return $this->fetch('apply/list');
        }
        
    }
    
    public function applyAction(){
        $params = paramForm();
        $apply = Apply::get($params['id']);
        $member= Member::get($apply->user_id);
        
        //送上门的信息不要浪费
        if(!$member->email){
            $member->email = $apply->email;
            $member->isUpdate()->save();
        }
        
        if($params['status']!=1){
            $apply->status =$params['status'];
            $apply->isUpdate()->save();
            $this->success('操作成功','/seller/apply');
            die;
        }
        
        
    
        $apply->status = 1;
        $apply->isUpdate()->save();
        
        //新建商家
        $company = new Company();
        $company->company = $apply->name;
        $company->user_id = $member->id;
        //电话和email都复制过来。商家可以自己设置，以后在用到商家信息的时候。就可以和普通用户区分开了
        $company->telephone = $member->phone;
        $company->email = $member->email;
        $rt = $company->save();
        
        //修改用户表
        if($rt){
            $member->co_id = $company->id;
            $member->co_name = $company->company;
            $member->isUpdate()->save();
        }
        $this->success('操作成功','/seller/apply');
        
        
        
        
    }
    
}
